package com.xiangyueEducation.uploaderCloud.controller;


import com.xiangyueEducation.uploaderCloud.utils.AliOssUtil;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.utils.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file)
    {
        log.info("文件上传{}",file);
        try{
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID()+extension;
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.ok(filePath);
        }catch (Exception e){
            log.info("文件上传失败{}",e);
        }
        return Result.build(null, ResultCodeEnum.FILE_MAKE_ERROR);
    }

}
